# Maintainer: Levente Polyak <anthraxx[at]archlinux[dot]org>
# Contributor: Sergej Pupykin <pupykin.s+arch@gmail.com>
# Contributor: William Rea <sillywilly@gmail.com>

pkgname=maven
pkgver=3.8.7
pkgrel=1
pkgdesc='Java project management and project comprehension tool'
url='https://maven.apache.org'
arch=('any')
license=('APACHE')
depends=('java-environment>=8' 'bash' 'procps-ng')
makedepends=('maven' 'java-environment=8')
backup=('opt/maven/conf/settings.xml')
source=(https://downloads.apache.org/maven/maven-3/${pkgver}/source/apache-maven-${pkgver}-src.tar.gz{,.asc}
        # both bin artifacts are only used for reproducible builds from source
        https://downloads.apache.org/maven/maven-3/${pkgver}/binaries/apache-maven-${pkgver}-bin.tar.gz{,.asc,.sha512}
        maven.sh)
noextract=(apache-maven-${pkgver}-bin.tar.gz)
# https://www.apache.org/dist/maven/KEYS
validpgpkeys=(
  '042B29E928995B9DB963C636C7CA19B7B620D787' # StephenConnolly<stephenc@apache.org>
  'AE9E53FC28FF2AB1012273D0BF1518E0160788A2' # Karl Heinz Marbaise (ASF Key) <khmarbaise@apache.org>
  'BBE7232D7991050B54C8EA0ADC08637CA615D22C' # Enrico Olivelli <eolivelli@apache.org>
  'B02137D875D833D9B23392ECAE5A7FB608A0221C' # Robert Scholte <rfscholte@apache.org>
  '6A814B1F869C2BBEAB7CB7271A2A1C94BDE89688' # Michael Osipov (Java developer) <1983-01-06@gmx.net>
)
sha256sums=('fe13478988307e3160624e2f672fc3d63c7e528e78085885ee57342d693372e5'
            'SKIP'
            '628b49352130d1d25d5519b1c724f0efe58b86bad55f37a694ca8f73f11e3604'
            'SKIP'
            'ea9877cc10162f7503fc1ef29c6d81174c3eb0aa9de7d0b4381c9a40949b4213'
            '17d9b8efa6a922907110026273443bcc8d83e061e14ac82cb5052b483acc9b76')

_buildnumber() {
  # use same build number as the binary dist but prove that our source build is
  # identical and therefor canonical in the build() step
  bsdtar xOf <(bsdtar xOf "${srcdir}/apache-maven-${pkgver}-bin.tar.gz" apache-maven-${pkgver}/lib/maven-core-${pkgver}.jar) \
    org/apache/maven/messages/build.properties | grep buildNumber | cut -d= -f2 | tr -cd '[:print:]'
}

build() {
  cd apache-maven-${pkgver}

  export PATH="/usr/lib/jvm/java-8-openjdk/bin:${PATH}"
  mvn package \
    -DbuildNumber="$(_buildnumber)" \
    -Dline.separator=$'\r\n' \
    -Dproject.build.sourceEncoding=UTF-8 -e \
    -Dmaven.repo.local="${srcdir}/repo" \
    -DskipTests

  # check reproducible builds result against upstream hashes. This way we can
  # prove that our source build is identical and therefor canonical so we are
  # technically free to use the static build number in our build env. On top we
  # ensure bit by bit identical upstream signed binary dist against our variant
  # via diff exiting non-successful on mismatch.
#sha512sum -c <(printf "$(cat ${srcdir}/apache-maven-${pkgver}-bin.tar.gz.sha512) apache-maven/target/apache-maven-${pkgver}-bin.tar.gz")
#diff "${srcdir}/apache-maven-${pkgver}-bin.tar.gz" apache-maven/target/apache-maven-${pkgver}-bin.tar.gz
}

check() {
  cd apache-maven-${pkgver}

  export PATH="/usr/lib/jvm/java-8-openjdk/bin:${PATH}"
  mvn test \
    -Dmaven.repo.local="${srcdir}/repo"
}

package() {
  cd apache-maven-${pkgver}

  install -Dm 755 ../maven.sh -t "${pkgdir}/etc/profile.d"

  install -d "${pkgdir}/opt/maven"
  bsdtar xf apache-maven/target/apache-maven-${pkgver}-bin.tar.gz \
    --strip-components=1 \
    -C "${pkgdir}/opt/maven"

  install -d "${pkgdir}/usr/bin"
  ln -s /opt/maven/bin/mvn "${pkgdir}/usr/bin/mvn"
  ln -s /opt/maven/bin/mvnDebug "${pkgdir}/usr/bin/mvnDebug"
}

# vim: ts=2 sw=2 et:
